<?php


namespace app\admin\controller;


use app\common\controller\Common;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Cartoon extends Controller
{
    public function _initialize()
    {
        //获取平台标题
        $platform = Db::name("config_platform")->find();
        $this->assign("platform", $platform);
        $swidthStyle = Session::get("style");
        $this->assign("swidthStyle",$swidthStyle);
    }
    //动画列表
    public function index()
    {
        //权限查询
        $common = new Common();
        $powerList = $common->showpower();
        $this->assign("powerList",$powerList);

        $thisWechat = Session::get("thisWechat");
        $where['uniacid'] = $thisWechat['id'];
        if (!empty($_GET['type'])) {
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $list = Db::name("admin_cartoon")->where($where)->limit($start,$limit)->order("sort desc,id desc")->select();

                foreach ($list as $k=>$v){
                    $list[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
                    $v['thumb'] = empty($v['thumb']) ? "/static/images/nopic-107.png" : $v['thumb'];
                    $list[$k]['thumb'] = "<img src='".$v['thumb']."' style='height:50px'>";
                }

                $count = Db::name("admin_cartoon")->where($where)->count("*");
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }
        return $this->fetch('index');
    }

    //详细信息
    public function detail(){
        //审核状态
        $audit = Db::name("config_attribute")->where(['key' => "enable_status"])->value("value");
        $audit = explode("|", $audit);
        foreach ($audit as $k => $v) {
            $audit[$k + 1] = $v;
        }
        unset($audit[0]);
        $this->assign("audit",$audit);

        if(empty($_GET['id'])){
            return $this->fetch('addData');
        }else {
            $id = $_GET['id'];
            $where['id'] = $id;
            $detail = Db::name("admin_cartoon")->where($where)->find();
            $this->assign("detail",$detail);
            return $this->fetch('editData');
        }
    }

    //添加修改操作
    public function doCartoon(){
        $common = new Common();
        try {
            $rule = [
                ["title", "require", "主题不能为空"],
                ["thumb", "require", "背景图不能为空"],
            ];
            $validate = new Validate($rule);
            $value = $this->request->post();
            $thisWechat = Session::get("thisWechat");
            $value['uniacid'] = $thisWechat['id'];
            if (!$validate->check($value)) {
                throw new Exception($validate->getError());
            }
            if (empty($value['id'])) {
                $value['addtime'] = time();
                $value['add_id'] = Session::get("id");
                $id = Db::name("admin_cartoon")->insertGetId($value);
                $log['remark'] = "用户【" . Session::get("user_name") . "】添加平台动画【".$value['title']."】,关联id【" . $id . "】";
            } else {
                $id = $value['id'];
                $value['updatetime'] = time();
                $value['update_id'] = Session::get("id");
                Db::name("admin_cartoon")->update($value);
                $log['remark'] = "用户【" . Session::get("user_name") . "】更新平台动画【".$value['title']."】的信息,关联id【" . $id . "】";
            }
            $log['controller'] = "Cartoon";
            $log['methods'] = "doCartoon";
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //删除平台动画
    public function delCartoon(){
        $common = new Common();
        try {
            $id = $_POST['id'];
            $navList = Db::name("admin_cartoon")->where(['id' => ['in', $id]])->field("id,title")->select();
            foreach ($navList as $k=>$v){
                $log['remark'] = "账号【" . Session::get("user_name") . "】删除平台动画信息,主题【".$v['title']."】删除id【" . $v['id'] . "】";
                $log['controller'] = "Cartoon";
                $log['methods'] = "delCartoon";
                $common->addLog($log);
            }
            Db::name("admin_cartoon")->where(['id' => ['in', $id]])->delete();
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

}